package com.jni.adapters;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.NameValuePair;
import org.json.JSONObject;

import android.content.ClipData.Item;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.jni.model.Lnews;
import com.jni.parsers.LastNewsParser;
import com.jni.riyada.LayoutThree;
import com.jni.ws.JSONParser;
import com.jni.ws.WsResonseLastNews;

public class LoadLastNews extends   AsyncTask<String, Void, List<Lnews>> { 
	//Context
	private Context context;
	public LoadLastNews(Context context){
		this.context = context;
	}
    protected List<Lnews> doInBackground(String... args) { 
    	LastNewsParser newsParser = null;
    	Log.i("LayoutThree","ok");
		JSONParser parser = new JSONParser();
		Log.i("LayoutThree","parser");
		List<NameValuePair> params = null;
		URL url;
		try {
			Log.i("LayoutThree","inside try");
			url = new URL("http://riyada.ma/Json_lastnews.php");
			URI uri;
			uri = new URI(url.getProtocol(), url.getHost(),
					url.getPath(), url.getQuery(), null);
			Log.i("LayoutThree",uri.toString());
			String jsonResult = parser.makeHttpRequest(uri.toString(), "GET", params);
			Log.i("LayoutThree",jsonResult);
			newsParser = new LastNewsParser(jsonResult);
			Log.i("ok","inside diInBackground");
			
			/*for(Lnews news : newsParser.wsRespLNews.Lnews){
				Log.i("ok",""+news.article.title);
				article_text.setText(news.article.title);
			}*/
		
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}     
	return newsParser.wsRespLNews.Lnews ; 
    } 
    
 // publishProgress(json); 
    protected void onProgressUpdate (JSONObject... json) { 
     // ((ArrayAdapter) getListAdapter()).add (item[0]); 
    } 
    
    protected void onPostExecute (ArrayList<Lnews> array) { 
 	   //Log.e("items",items.toString());
 	  // EntryAdapter adapter = new EntryAdapter(LarynxSpeaker.this, items);
        
       // setListAdapter(adapter);
    	//NewsAdapter adapter = new NewsAdapter(context,1,array);
    	
    	 
      
      
      
    } 
  }
